package com.example.widgettest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;

public class AppWidget extends AppWidgetProvider {

	private String mText="aaa";
	private final static String TAG=AppWidget.class.getName();
	private final static String FILENAME="/baiduNote.txt";
	
	private Context cx;
	private AppWidgetManager appWidgetMge;
	
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {

			updateAppWidget(cx, appWidgetMge);
			super.handleMessage(msg);
		}
		
	};
	
	
	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}



	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

//		Intent intent=new Intent(context,UpdateService.class);
//		context.startService(intent);
//		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
//		final int N = appWidgetIds.length;  
//        for (int i = 0; i < N; i++){
//        	Intent intent=new Intent(context, MainActivity.class);
////    		intent.setClass();
//    		
//    		PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
//    		
//    		RemoteViews remoteViews= new RemoteViews(context.getPackageName(),R.layout.widget_layout);
//    		mText=read();
//    		remoteViews.setTextViewText(R.id.txt1, mText);
//    		remoteViews.setOnClickPendingIntent(R.id.txt1, pendingIntent);
//    		
//    		
//    		ComponentName componentName=new ComponentName(context,AppWidget.class);
//    		appWidgetManager.updateAppWidget(componentName, remoteViews);
		updateAppWidget(context, appWidgetManager);
		this.cx=context;
		this.appWidgetMge=appWidgetManager;
				
		MainActivity.handler=handler;
		
//    		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);	
//        }
    		
//		Intent intent=new Intent();
//		intent.setClass(context, MainActivity.class);
//		
//		PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
//		
//		RemoteViews remoteViews= new RemoteViews(context.getPackageName(),R.layout.widget_layout);
//		remoteViews.setTextViewText(R.id.txt1, "00000000000000000000");
//		remoteViews.setOnClickPendingIntent(R.id.txt1, pendingIntent);
//		
//		
//		ComponentName componentName=new ComponentName(context,AppWidget.class);
//		appWidgetManager.updateAppWidget(componentName, remoteViews);
		
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

//	public static class UpdateService extends Service{
//
//		@Override
//		public IBinder onBind(Intent intent) {
//
//			return null;
//		}
//
//		@Override
//		@Deprecated
//		public void onStart(Intent intent, int startId) {
//			// TODO Auto-generated method stub
//			super.onStart(intent, startId);
//			RemoteViews updateViews=new RemoteViews(this.getPackageName(),R.layout.widget_layout);
//			
////			SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
//			String aaa="ffffffffffffffffffffffffffff";
//			
//			updateViews.setTextViewText(R.id.txt1,""+aaa);
//			
//			ComponentName thisWidget=new ComponentName(this, AppWidget.class);
//			AppWidgetManager manager=AppWidgetManager.getInstance(this);
//			manager.updateAppWidget(thisWidget, updateViews);
//		}
//		
//	}

	public void updateAppWidget(Context context,
			AppWidgetManager appWidgetManager){
		Intent intent=new Intent(context, MainActivity.class);
//		intent.setClass();
		Log.e(TAG,"updateappwidget");
		PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
		
		RemoteViews remoteViews= new RemoteViews(context.getPackageName(),R.layout.widget_layout);
		mText=read();
		remoteViews.setTextViewText(R.id.txt1, mText);
		remoteViews.setOnClickPendingIntent(R.id.txt1, pendingIntent);
		
		
		ComponentName componentName=new ComponentName(context,AppWidget.class);
		appWidgetManager.updateAppWidget(componentName, remoteViews);
	}
	
	
	private String read(){
		try {
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
				File sdDir=Environment.getExternalStorageDirectory();
				FileInputStream fis=new FileInputStream(sdDir.getCanonicalPath()+FILENAME);
				BufferedReader br=new BufferedReader(new InputStreamReader(fis));
				StringBuilder sb=new StringBuilder("");
				String line=null;
				while((line=br.readLine())!=null){
					sb.append(line);
				}
				br.close();
				return sb.toString();
			}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return null;
	}




}
